
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px" v-loading="loading">
                             <el-form-item label="标题" prop="title">
                        <el-input v-model.trim="data.title" placeholder="请填写标题" />
                    </el-form-item>
                    <el-form-item label="分类" prop="type">
                    <el-select v-model.trim="data.type" placeholder="请选择分类" clearable  class="filter-item" >
                        <el-option
                            v-for="item in type"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id">
                          </el-option>
                    </el-select>
                    </el-form-item>
                    <el-form-item label="详情" prop="detail">
                      <editor v-model="data.detail" v-if="this.item.dialogVisible"/> 
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="data.sort" :min="1" :max="10" label="排序"></el-input-number>
                    </el-form-item>
                    <el-form-item label="图片" prop="thumb" >
                        <el-upload
                    :action="$store.state.settings.file_upload_url"
                    :headers ="headerObj"
                    :limit=1
                    list-type="picture-card"
                    :file-list="data.thumbfileList"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                    :on-success="handlethumbSuccess"
                    :before-upload="beforethumbUpload"
                    :on-exceed="handlethumbExceed"
                    :on-preview="handlethumbPreview"
                    :on-remove="handlethumbRemove">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                    </el-form-item>
                    <el-form-item label="小标题" prop="sub_title">
                        <el-input v-model.trim="data.sub_title" placeholder="请填写小标题" />
                    </el-form-item>
                    <el-form-item label="是否显示">
                             <el-switch v-model="data.is_show"></el-switch>
                    </el-form-item>
                    <el-form-item label="是否置顶">
                             <el-switch v-model="data.is_top"></el-switch>
                    </el-form-item>
  
          <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
          </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {detail,edit,get_type} from '@/api/notice'
import { getToken } from '@/utils/auth'
import  editor  from "@/components/Editor";



export default {
  name: 'NoticeEdit',
  components: {editor },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      type: [],
      loading:true,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    title: [ 
                { required: true, message: "请输入标题", trigger: "blur" }
            ], 
            type: [ 
                { required: true, message: "请输入分类", trigger: "blur" }
            ], 
            detail: [ 
                { required: true, message: "请输入详情", trigger: "blur" }
            ], 
            thumb: [ 
                { required: true, message: "请输入图片", trigger: "blur" }
            ], 

      },
      data: {
                        title:'',
            type:'',
            detail:'',
            sort:'',
            thumb:[],
            thumbpreviewurl:'',
            thumbfileList:[],
            sub_title:'',
            is_show:true,
            is_top:true,

         }
    }
  },
  created() {
    const id = this.item.id
    this.id = id
    this.fetchData(id)
     this.get_type();
  },
  watch: {
    item: {
      handler(val, oldVal) {
        if (this.item.dialogVisible === true 
        ) {
          this.id = val.id
          this.fetchData(val.id)
        }
      },
      deep: true
    }
  },
  methods: {
          get_type(){
              get_type().then(res => {
                this.type = res.data;
              })
          },
                 handleInput(value) {
            },
             handleClick(e, editor) {
            },
             clear() {
                this.$refs.editor.clear()
            },
            handlethumbExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handlethumbPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handlethumbRemove(){
                 this.data.thumb = '';
             },
             handlethumbSuccess(response, file, fileList) {
               this.$message.success('上传成功');
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.thumb= response.data.src;
            },
             beforethumbUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传头像图片大小不能超过 2MB!");
                }
            },

    fetchData(id) {
          detail(id).then(response => {
            this.loading = false;
            this.data = response.data;
                        this.data.thumbfileList=response.data.thumb ? response.data.thumb.split(',').map(item=>{return{name:item,url:item}}):[];
            this.data.is_show=response.data.is_show==1 ?true:false;
            this.data.is_top=response.data.is_top==1 ?true:false;

          }).catch(err => {
            console.log(err)
          })
        },
    submit() {
      const thiz = this
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        edit({
          id: this.data.id,
                        title:this.data.title,
            type:this.data.type,
            detail:this.data.detail,
            sort:this.data.sort,
            thumb:this.data.thumb,
            sub_title:this.data.sub_title,
            is_show:this.data.is_show ?1:0,
            is_top:this.data.is_top ?1:0,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
